package book.spec;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class DESKeySpecTest {

	public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
		
		KeyGenerator gen = KeyGenerator.getInstance("DES");
		SecretKey key = gen.generateKey();
		byte[] keyBytes = key.getEncoded();
		
		//第一种方式
		SecretKey key2 = new SecretKeySpec(keyBytes, key.getAlgorithm());
		
		//第二种方式
		DESKeySpec spec = new DESKeySpec(keyBytes);
		
		SecretKeyFactory fac = SecretKeyFactory.getInstance(key.getAlgorithm());
		SecretKey key3 = fac.generateSecret(spec);
		System.out.println(key3.equals(key));
		System.out.println(key3.equals(key2));
	}
}
